<?php

require_once "include.php";

function distance($lat1, $lon1, $lat2, $lon2, $unit) { 

  $theta = $lon1 - $lon2; 
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
  $dist = acos($dist); 
  $dist = rad2deg($dist); 
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344); 
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}


if($method == "GET"){
	$query = "SELECT * FROM checkin";
	
	$user_id = mysql_real_escape_string($_REQUEST['user_id']);
	$longitude = mysql_real_escape_string($_REQUEST['longitude']);
	$latitude = mysql_real_escape_string($_REQUEST['latitude']);
	
	if($user_id && $longitude && $latitude){
		$query = "SELECT * FROM canteen";
		
		$result = mysql_query($query) or die(mysql_error());  

		$return_arr = array();
		
		$minDistance = PHP_INT_MAX;
		$minRow = null;
		while($row = mysql_fetch_array( $result )){
			$distance = distance($latitude, $longitude, $row["latitude"], $row["longitude"], "k");
			
			if($distance < $minDistance){
				$minDistance = $distance;
				$minRow = $row;
			}
		}
		
		
		$query = "SELECT * FROM stall WHERE canteen_id=" . $minRow["id"];
		
		$offer_arr = array();
		
		$result = mysql_query($query) or die(mysql_error());
		while($row = mysql_fetch_array( $result )){
			$query = "SELECT * FROM dish WHERE on_promotion=1 AND stall_id=" . $row["id"];
			$result2 = mysql_query($query) or die(mysql_error());
			
			while($row2 = mysql_fetch_array( $result2)){
				$offer = array(
							"id" => $row2["id"],
							"name" => $row2["name"],
							"image" => $row2["image"],
							"description" => $row2["description"],
							"rating" => $row2["rating"],
							"price" => $row2["price"],
							"on_promotion" => $row2["on_promotion"],
							"stall_id" => $row2["stall_id"]
							);
				$offer_arr[] = $offer;
			}
		}
		
		echo json_encode(array(
			"nearestDistance" => "$minDistance km",
			"nearestCanteen" => array(
					"id" => $minRow["id"],
					"name" => $minRow["name"],
					"image" => $minRow["image"],
					"description" => $minRow["description"],
					"latitude" => $minRow["latitude"],
					"longitude" => $minRow["longitude"],
					"no_of_stalls" => $minRow["no_of_stalls"]
					),
			"nearestOffers" => $offer_arr
					));
	} else{
		if($user_id){
			$ids = explode(",", $_REQUEST['user_id']);

			$i = 0;
			foreach($ids as $id){
				if($i == 0){
					$query .= " WHERE user_id=$id";
				} else{
					$query .= " OR user_id=$id";
				}

				$i++;
			}
		}
		else if($_REQUEST["stall_id"]){
			$ids = explode(",", $_REQUEST['stall_id']);

			$i = 0;
			foreach($ids as $id){
				if($i == 0){
					$query .= " WHERE stall_id=$id";
				} else{
					$query .= " OR stall_id=$id";
				}

				$i++;
		}
	}

	$result = mysql_query($query) or die(mysql_error());  

	$return_arr = array();

	while($row = mysql_fetch_array( $result )){
		$checkin = array(
					"id" => $row["id"],
					"user_id" => $row["user_id"],
					"stall_id" => $row["stall_id"]
					);

		$return_arr[] = $checkin;
	}

	echo json_encode($return_arr);
	}
}
else if($method == "POST"){
	$user_id = mysql_real_escape_string($_REQUEST['user_id']);
	$stall_id = mysql_real_escape_string($_REQUEST['stall_id']);
	
	
	if($user_id && $stall_id){
		$query = "INSERT INTO checkin(user_id, stall_id) VALUES($user_id, $stall_id)";
		
		$result = mysql_query($query);
		if($result){
			echo json_encode("true");
		}else{
			$error = array("error" =>  mysql_error());
			echo json_encode($error);
		}
	}
}
else if($method == "DELETE"){
	if($_REQUEST["id"]){
		$id = mysql_real_escape_string($_REQUEST["id"]);
		$query = "DELETE FROM checkin WHERE id=$id";
		
		$result = mysql_query($query);
		if($result){
			echo json_encode("true");
		}else{
			$error = array("error" =>  mysql_error());
			echo json_encode($error);
		}
	}
}

?>